<?php

class Users extends Controller {

    function login() {
        if (isset($_POST["username"]) && isset($_POST["password"])) {
            $username = $_POST["username"];
            $password = $_POST["password"];

            $credentials = $this->User->verify_credentials($username, $password);

            if (count($credentials) != 0) {
                $blocked = $credentials["User"]["blocked"];
            }

            if ((count($credentials) > 0) && ($blocked != 1)) {
                $this->session->set("user_id", $credentials["User"]["id"]);
                $this->session->set("role_id", $credentials["User"]["role_id"]);
                $this->session->set("twitter_username", $credentials["User"]["twitter_username"]);
                $this->session->set("oauth_token", $credentials["User"]["oauth_token"]);
                $this->session->set("oauth_token_secret", $credentials["User"]["oauth_token_secret"]);

                $this->session->set("authenticated", true);

                $this->redirect("/");
                $this->User->set_login($credentials["User"]["id"]);
            } else {
                $this->session->set_flash("Login failed.");
            }
        }

        $this->set('title', 'Login');
    }

    function logout() {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $user_id = $this->session->get("user_id");
        $this->User->set_logout($user_id);
        $this->session->destroy();
        $this->session->set_flash("You have been logged out.");
        $this->_auto_render = false;
        $this->redirect("/users/login");
    }

    function signup() {
        if (isset($_POST["submit"])) {
            $user["username"] = $_POST["username"];
            $user["password"] = $_POST["password"];
            $user["name"] = $_POST["name"];
            $user["dob"] = $_POST["dob"];
            $user["email"] = $_POST["email"];
            $user["gender"] = $_POST["gender"];
            $user["location"] = $_POST["location"];
            $user["twitter_username"] = $_POST["twitter_username"];
            $user["oauth_token"] = $_POST["oauth_token"];
            $user["oauth_token_secret"] = $_POST["oauth_token_secret"];

            $this->User->add($user);
            $user_id = $this->User->insert_id();
            $this->User->add_status($user_id);

            if ($user["twitter_username"] !== '') {
                $profile_link = BASE_PATH . "/users/view/$user_id";
                $shortened_profile_link = $this->bitly->get_bitly_short_url($profile_link);
                $this->twitter->setOAuthToken($user["oauth_token"]);
                $this->twitter->setOAuthTokenSecret($user["oauth_token_secret"]);
                $this->twitter->statusesUpdate("Saya baru saja melakukan registrasi di Bekti Photo Sharing. Lihat profil saya di $shortened_profile_link.");
            }

            $this->session->set_flash("User registered successfully. You may login now.");
            $this->_auto_render = false;

            $this->redirect("/users/login");
        }

        $this->set('title', 'Sign Up');
    }

    function follow($user_id, $target_user_id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $this->User->follow($user_id, $target_user_id);
        $this->User->add_notification($user_id,$target_user_id);

        $this->_auto_render = false;
        $this->session->set_flash("You are now following this user.");
        $this->redirect("/users/view/$target_user_id");
    }

    function unfollow($user_id, $target_user_id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $this->User->unfollow($user_id, $target_user_id);

        $this->_auto_render = false;
        $this->session->set_flash("You are not following this user anymore.");
        $this->redirect("/users/view/$target_user_id");
    }

    function view($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $user = $this->User->select($id);

        $this->set("user", $user);
        $this->set("title", $user["User"]["name"]);
    }

    function profile_ajax($id) {
        $user = $this->User->select($id);
        $Photo = new Photo();
        $followers = $this->User->followers($id);
        $follows = $this->User->follows($this->session->get("user_id"), $id);

        $profile_photo = $Photo->select($user["User"]["photo_id"]);
        $profile_photo_filename = $profile_photo["Photo"]["filename"];

        $this->_render_header = false;
        $this->set("profile_photo", $profile_photo_filename);
        $this->set("user", $user);
        $this->set("follows", $follows);
        $this->set("followings", $this->User->followings_complete($id));
        $this->set("followers", $this->User->followers_complete($id));
    }

    function photos_ajax($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $this->_render_header = false;
    }

    function user_photos_ajax($id, $page = "1") {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $Photo = new Photo();
        $offset = ((int) $page - 1) * 7;
        $user_photos = $Photo->get_user_photos($id, $offset);
        $count = count($user_photos);

        for ($i = 0; $i < $count; $i++) {
            $access_rule = $user_photos[$i]["Photo"]["access_rule"];
            $author_user_id = $user_photos[$i]["Photo"]["author_user_id"];

            if ($access_rule == 4) {
                if (!($this->session->get("authenticated") && ($author_user_id == $this->session->get("user_id")))) {
                    unset($user_photos[$i]);
                }
            } else if ($access_rule == 3) {
                if (!$this->session->get("authenticated")) {
                    unset($user_photos[$i]);
                }

                if (!$this->User->follows($this->session->get("user_id"), $author_user_id) && ($author_user_id != $this->session->get("user_id"))) {
                    unset($user_photos[$i]);
                }
            } else if ($access_rule == 2) {
                if (!$this->session->get("authenticated")) {
                    unset($user_photos[$i]);
                }
            }
        }

        $user_photos = array_values($user_photos);

        $this->_render_header = false;
        $this->set("user_photos", $user_photos);
    }

    function wall_ajax($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $user = $this->User->select($id);
        $wall_rule = $user["User"]["wall_rule"];

        $comment_enabled = false;

        if (($wall_rule == "2") && ($id == $this->session->get("user_id"))) {
            $comment_enabled = true;
        } else if ((($wall_rule == "1") && (in_array($this->session->get("user_id"), $followers))) || ($id == $this->session->get("user_id"))) {
            $comment_enabled = true;
        } else if ($wall_rule == "0") {
            $comment_enabled = true;
        }

        $this->_render_header = false;
        $this->set("user", $user);
        $this->set("comment_enabled", $comment_enabled);
    }

    function user_wall_ajax($id, $page = "1") {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $user = $this->User->select($id);
        $offset = ((int) $page - 1) * 5;
        $wall_posts = $this->User->get_wall($id, $offset);
        $wall_rule = $user["User"]["wall_rule"];

        $comment_enabled = false;

        if (($wall_rule == "2") && ($id == $this->session->get("user_id"))) {
            $comment_enabled = true;
        } else if ((($wall_rule == "1") && (in_array($this->session->get("user_id"), $followers))) || ($id == $this->session->get("user_id"))) {
            $comment_enabled = true;
        } else if ($wall_rule == "0") {
            $comment_enabled = true;
        }

        $this->_render_header = false;
        $this->set("wall_posts", $wall_posts);
        $this->set("user", $user);
        $this->set("comment_enabled", $comment_enabled);
    }

    function wall($method, $param1, $param2 = NULL) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        if ($method == "delete") {
            $this->User->delete_wall($param2);
            $this->session->set_flash("Your wall post has been deleted.");
        } else if ($method == "add") {
            $wall["author_user_id"] = $this->session->get("user_id");
            $wall["recipient_user_id"] = $param1;
            $wall["content"] = $_POST["content_add_wall"];

            $this->User->add_wall($wall);
            $this->session->set_flash("Your wall post has been saved.");
        } else if ($method == "delete_comment") {
            $this->User->delete_wall_comment($param2);
            $this->session->set_flash("Your comment has been deleted.");
        } else if ($method == "add_comment") {
            $comment["author_user_id"] = $this->session->get("user_id");
            $comment["post_id"] = $param2;
            $comment["content"] = $_POST["content_add_wall_comment"];

            $this->User->add_wall_comment($comment);
            $this->session->set_flash("Your comment has been saved.");
        }

        $this->_auto_render = false;
        $this->redirect("/users/view/$param1");
    }

    function edit($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        if (isset($_POST["submit"])) {
            $user["id"] = $id;
            $user["password"] = $_POST["password"];
            $user["name"] = $_POST["name"];
            $user["dob"] = $_POST["dob"];
            $user["email"] = $_POST["email"];
            $user["gender"] = $_POST["gender"];
            $user["location"] = $_POST["location"];
            $user["wall_rule"] = $_POST["wall_rule"];
            $user["twitter_username"] = $_POST["twitter_username"];
            $user["oauth_token"] = $_POST["oauth_token"];
            $user["oauth_token_secret"] = $_POST["oauth_token_secret"];

            if (isset($_POST["blocked"])) {
                $user["blocked"] = '1';
            } else {
                $user["blocked"] = '0';
            }

            $this->User->update($user);

            $this->session->set("twitter_username", $user["twitter_username"]);
            $this->session->set("oauth_token", $user["oauth_token"]);
            $this->session->set("oauth_token_secret", $user["oauth_token_secret"]);

            $this->session->set_flash("User profile has been updated successfully.");
            $this->_auto_render = false;

            $this->redirect("/users/view/$id");
        }

        $this->set("title", "Edit Profile");
        $this->set("user", $this->User->select($id));
    }

    function set_photo($user_id, $photo_id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $this->User->set_photo($user_id, $photo_id);

        $this->session->set_flash("Your display picture has been updated successfully.");
        $this->_auto_render = false;

        $this->redirect("/users/view/$user_id");
    }

    function integrate() {
        $this->_render_header = false;
    }

    function test_tweet() {
        $this->_render_header = false;
        $status = "Selamat malam dari kandang";
        $statuses = $this->twitter->statusesUpdate($status);
        die(print_r($statuses));
    }

    function get_token($oauth_token, $oauth_verifier) {
        $this->_render_header = false;
        $this->set("oauth_token", $oauth_token);
        $this->set("oauth_verifier", $oauth_verifier);
    }

    function notification($id) {
        if (!$this->session->get("authenticated")) {
            return;
        }
        
        $notif = $this->User->notification($id);

        $notif_count = count($notif);

        $this->_auto_render = false;
        echo $notif_count;
    }

    function notification_feed($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $notif = $this->User->notification($id);
        $this->_render_header = false;
        $this->set("list_notif", $notif);
    }

    function notification_delete($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $this->User->notification_delete($id);
        $this->_auto_render = false;
    }

    function chat_ajax($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $this->_render_header = false;
        $user = $this->User->chat_ajax($id);
        $this->set("user", $user);
    }

    function get_user($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $this->_render_header = false;
        $user = $this->User->get_user($id);
        echo $user['User']['name'];
    }

    function submit_chat($to, $content) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $from = $this->session->get("user_id");
        $this->User->submit_chat($from, $to, $content);
        $this->_render_header = false;
    }

    function get_chat($recipient) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $id = $this->session->get("user_id");
        $chat = $this->User->get_chat($id, $recipient);
        $this->_render_header = false;
        $this->set("chats", $chat);
    }

    function get_online($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $user = $this->User->get_online($id);
        $this->_render_header = false;
        echo $user['Online']['status'];
    }
}